package com.fulihui.wgj.service.retention;

import com.alibaba.dubbo.config.annotation.Service;
import com.fulihui.common.builder.rpc.RpcResultBuilder;
import com.fulihui.common.conv.BeanConv;
import com.fulihui.common.error.Errors;
import com.fulihui.common.error.InvokeServiceException;
import com.fulihui.common.result.rpc.BaseResult;
import com.fulihui.common.result.rpc.SimpleResult;
import com.fulihui.common.util.ConditionValidator;
import com.fulihui.wgj.dal.mybatis.domain.WxSevenRetention;
import com.fulihui.wgj.dal.mybatis.repository.WxSevenRetentionRepository;
import com.fulihui.wgj.facade.dto.WxSevenRetentionDTO;
import com.fulihui.wgj.facade.request.retention.WxSevenRetentionPageRequest;
import com.fulihui.wgj.facade.request.retention.WxSevenRetentionQueryRequest;
import com.fulihui.wgj.facade.request.retention.WxSevenRetentionRequest;
import com.fulihui.wgj.facade.result.retention.WxSevenRetentionPageResult;
import com.fulihui.wgj.facade.service.retention.WxSevenRetentionService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 微信粉丝七日留存Service
 *
 * @author wang_wx
 * @date 2018-07-03
 */
@Component
@Service(interfaceClass = WxSevenRetentionService.class, version = "1.0.0")
public class WxSevenRetentionServiceImpl implements WxSevenRetentionService {

    private static final Logger LOGGER = LoggerFactory.getLogger(WxSevenRetentionServiceImpl.class);

    @Autowired
    WxSevenRetentionRepository wxSevenRetentionRepository;

    @Override
    public BaseResult saveWxSevenRetention(WxSevenRetentionRequest request) throws InvokeServiceException {
        ConditionValidator.validate(request);
        WxSevenRetention wxSevenRetention = BeanConv.copy(request, WxSevenRetention.class);
        int exist = wxSevenRetentionRepository.countByAppIdAndDate(wxSevenRetention);
        if (exist > 0) {
            return RpcResultBuilder.fail(Errors.WxSevenRetentionError.RECORD_ALREADY_EXISTS);
        }
        Integer count = wxSevenRetentionRepository.saveSelective(wxSevenRetention);
        return count > 0 ? RpcResultBuilder.succ() : RpcResultBuilder.fail(Errors.Commons.SAVE_FAILED);
    }

    @Override
    public BaseResult updateByAppIdAndDate(WxSevenRetentionRequest request) throws InvokeServiceException {
        ConditionValidator.validate(request);
        WxSevenRetention wxSevenRetention = BeanConv.copy(request, WxSevenRetention.class);
        int count = wxSevenRetentionRepository.updateByAppIdAndDate(wxSevenRetention);
        return count > 0 ? RpcResultBuilder.succ() : RpcResultBuilder.fail(Errors.Commons.UPDATE_FAILED);
    }

    @Override
    public SimpleResult<List<WxSevenRetentionDTO>> queryWxSevenRetentions(WxSevenRetentionQueryRequest request) throws InvokeServiceException {
        ConditionValidator.validate(request);
        List<WxSevenRetentionDTO> wxSevenRetentions = wxSevenRetentionRepository.queryListDTO(request.getAppId(), request.getBeginDate(), request.getEndDate());
        SimpleResult<List<WxSevenRetentionDTO>> result = RpcResultBuilder.succSimple(wxSevenRetentions);
        return result;
    }

    @Override
    public WxSevenRetentionPageResult queryPageWxSevenRetentions(WxSevenRetentionPageRequest request) throws InvokeServiceException {
        ConditionValidator.validate(request);
        PageInfo<WxSevenRetentionDTO> pageInfo = wxSevenRetentionRepository.queryPageListDTO(request.getAppId(), request.getBeginDate(), request.getEndDate(), request.getPage(), request.getRows());
        long total = pageInfo.getTotal();
        WxSevenRetentionPageResult result = RpcResultBuilder.succPageResult(WxSevenRetentionPageResult.class, pageInfo.getPageNum(), pageInfo.getPageSize(), Math.toIntExact(total));
        result.setValue(pageInfo.getList());
        return result;
    }
}
